package com.appspot.didit.test.datastore;

import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;
import com.appspot.didit.client.workitem.Workitem;
import com.appspot.didit.server.PMF;

public class WorkitemDatastoreTestCase extends LocalDatastoreTestCase {

	PersistenceManager pm = PMF.get().getPersistenceManager();
	
	public void testInsertAndSelect() {
	    try {
	    	//Initiation
	    	/*Identity id = new Identity("12345678901234567");
			pm.makePersistent(id);
			Long user_id = id.getId();
			//******************************
			Date date = new Date();
			Workitem wi = new Workitem("test", 1.0, "tag", date, user_id);
			pm.makePersistent(wi);
			List<Workitem> workitems = getResults(user_id);
	    	assertNotNull(workitems);
	    	assertEquals(1, workitems.size());
	    	
	    	Workitem wi1 = workitems.get(0);
	    	assertEquals(wi.getId(), wi1.getId());	
			assertEquals("test", wi1.getDescription());
			assertEquals(1.0, wi1.getDuration());	
			assertEquals("tag", wi1.getTagString());
			assertEquals(date, wi1.getDate());
			assertEquals(user_id, wi1.getUser_id());*/
	    } catch (Exception e) {
	    	System.out.println(e.toString());
	    	assertTrue(false);
	    } finally {
	      pm.close();
	    }
	}
	
	public void testDelete() {

	    try {
	    	//Initiation
	    	/*Identity id = new Identity("12345678901234567");
			pm.makePersistent(id);
			Long user_id = id.getId();
			Date date = new Date();
			Workitem wi = new Workitem("test", 1, "tag", date, user_id);
			pm.makePersistent(wi);
			Workitem wi1 = pm.getObjectById(Workitem.class, wi.getId());
			//******************************
			pm.deletePersistent(wi1);
			
			List<Workitem> workitems = getResults(user_id);
	    	assertNotNull(workitems);
	    	assertEquals(0, workitems.size());*/
	    } catch (Exception e) {
	    	System.out.println(e.toString());
	    	assertTrue(false);
	    } finally {
	      pm.close();
	    }
	}
	
	public void testUpdate() {
	    try {
	    	//Initiation
	    	/*Identity id = new Identity("12345678901234567");
			pm.makePersistent(id);
			Long user_id = id.getId();
			Date date = new Date();
			Date date1 = new Date(0L);
			Workitem wi = new Workitem("test", 1.0, "tag", date, user_id);
			pm.makePersistent(wi);
			wi.setDescription("test1");
			wi.setTagString("tag1 tag2");
			wi.setDuration(2.0);
			wi.setDate(date1);
			//******************************
			
			Workitem wi1 = pm.getObjectById(Workitem.class, wi.getId());
			assertEquals("test1", wi1.getDescription());
			assertEquals(2.0, wi1.getDuration());	
			assertEquals("tag1 tag2", wi1.getTagString());
			assertEquals(date1, wi1.getDate());	*/
	    } catch (Exception e) {
	    	System.out.println(e.toString());
	    	assertTrue(false);
	    } finally {
	      pm.close();
	    }
	}
	
	private List<Workitem> getResults(Long user_id) {
		String sql = "SELECT FROM " + Workitem.class.getName() + " where user_id == " + user_id;
		List<Workitem> results = (List<Workitem>) pm.newQuery(sql).execute();
		return results;
	}
}
